<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="categoryName"
            placeholder="请输入分类名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="cateOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            ref="tree"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
            :filter-node-method="filterNode"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <!-- 搜索工作栏 -->
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="课程名称" prop="knowledgeName">
            <el-input
              v-model="queryParams.knowledgeName"
              placeholder="请输入课程名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="是否付费" prop="isPay">
            <el-input
              v-model="queryParams.isPay"
              placeholder="请输入是否付费"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <!-- 操作工具栏 -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['exam:course:create']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              :loading="exportLoading"
              v-hasPermi="['exam:course:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <!-- 列表 -->
        <el-table v-loading="loading" :data="list">
          <el-table-column label="编号" align="center" prop="id"/>
          <el-table-column label="知识类型" align="center" prop="type">
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.COURSE_TYPE" :value="scope.row.type"/>
            </template>
          </el-table-column>
          <el-table-column label="课程名称" align="center" prop="knowledgeName"/>

          <!-- <el-table-column label="到期时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
          </el-table-column>-->
          <!-- <el-table-column label="自定义访问量" align="center" prop="customCount"/> -->
          <el-table-column label="讲师" align="center" prop="teacher"/>
          <!-- <el-table-column label="是否付费" align="center" prop="isPay">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.IS_TRUE" :value="scope.row.isPay"/>
        </template>
      </el-table-column> 
          <el-table-column label="金额" align="center" prop="amount"/>-->
          <!-- <el-table-column label="是否首页展示" align="center" prop="isShow">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.IS_TRUE" :value="scope.row.isShow"/>
        </template>
          </el-table-column>-->
          <el-table-column label="是否公开" align="center" prop="isPulice">
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.IS_PUBLIC" :value="scope.row.isPulice"/>
            </template>
          </el-table-column>
          <el-table-column label="访问量" align="center" prop="count"/>
          <!-- <el-table-column label="审核人" align="center" prop="reviewer" /> -->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['exam:course:update']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['exam:course:delete']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 对话框(添加 / 修改) -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="知识类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择知识类型" @change="typeChange">
                <el-option
                  v-for="dict in this.getDictDatas(DICT_TYPE.COURSE_TYPE)"
                  :key="dict.value"
                  :value="parseInt(dict.value)"
                  :label="dict.label"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="所属类目" prop="categoryId">
              <treeselect
                v-model="form.categoryId"
                :options="cateOptions"
                :show-count="true"
                :clearable="false"
                placeholder="请选择归属分类"
                :normalizer="normalizer"
              />
            </el-form-item>
            <el-form-item label="课程名称" prop="knowledgeName">
              <el-input v-model="form.knowledgeName" placeholder="请输入课程名称"/>
            </el-form-item>
            <el-form-item label="封面海报" prop="imgUrl" v-show="videoShow">
              <imageUpload v-model="form.imgUrl"/>
            </el-form-item>

            <el-form-item label="课程内容" prop="knowledgeDesc" v-show="imgShow">
              <Editor v-model="form.knowledgeDesc"/>
            </el-form-item>
            <!-- <el-form-item label="附件ID" prop="fileId">
          <el-input v-model="form.fileId" placeholder="请输入附件ID" />
        </el-form-item>
        <el-form-item label="审核状态" prop="status">
          <el-select v-model="form.status" placeholder="是否通过审核">
            <el-option  v-for="dict in this.getDictDatas(DICT_TYPE.EXAMINE_STATUS)" :key="dict.value"  :value="dict.value" :label="dict.label"/>
          </el-select>
            </el-form-item>-->
            <el-form-item label="视频文件" prop="videoUrl" v-show="videoShow">
              <FileUpload v-model="form.videoUrl" :fileType="fileType" :fileSize="fileSize"/>
            </el-form-item>

            <!-- <el-form-item label="到期时间" prop="endTime">
          <el-date-picker clearable v-model="form.endTime" type="date" value-format="timestamp"
                          placeholder="选择到期时间"/>
            </el-form-item>-->
            <!-- <el-form-item label="自定义访问量" prop="customCount">
          <el-input v-model="form.customCount" placeholder="请输入自定义访问量"/>
            </el-form-item>-->
            <el-form-item label="讲师" prop="teacher">
              <el-input v-model="form.teacher" placeholder="请输入讲师"/>
            </el-form-item>
            <!--        <el-form-item label="是否付费" prop="isPay">-->
            <!--          <el-radio-group v-model="form.isPay">-->
            <!--            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.IS_TRUE)"-->
            <!--                      :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}-->
            <!--            </el-radio>-->
            <!--          </el-radio-group>-->
            <!--        </el-form-item>-->
            <!--        <el-form-item label="金额" prop="amount">-->
            <!--          <el-input v-model="form.amount" placeholder="请输入金额"/>-->
            <!--        </el-form-item>-->
            <!-- <el-form-item label="是否首页展示" prop="isShow">
          <el-radio-group v-model="form.isShow">
            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.IS_TRUE)"
                      :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}
            </el-radio>
          </el-radio-group>
            </el-form-item>-->
            <!--        <el-form-item label="是否公开" prop="isPulice">-->
            <!--          <el-radio-group v-model="form.isPulice">-->
            <!--            <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.IS_PUBLIC)"-->
            <!--                      :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}-->
            <!--            </el-radio>-->
            <!--          </el-radio-group>-->
            <!--        </el-form-item>-->
            <!--        <el-form-item label="访问量" prop="count">-->
            <!--          <el-input v-model="form.count" placeholder="请输入访问量"/>-->
            <!--        </el-form-item>-->
            <!-- <el-form-item label="审核人" prop="reviewer">
          <el-input v-model="form.reviewer" placeholder="请输入审核人" />
            </el-form-item>-->
            <!-- <el-form-item label="创建时间" prop="createDate">
          <el-date-picker clearable v-model="form.createDate" type="date" value-format="timestamp" placeholder="选择创建时间" />
        </el-form-item>
        <el-form-item label="修改人" prop="modifier">
          <el-input v-model="form.modifier" placeholder="请输入修改人" />
        </el-form-item>
        <el-form-item label="修改时间" prop="modifyDate">
          <el-date-picker clearable v-model="form.modifyDate" type="date" value-format="timestamp" placeholder="选择修改时间" />
            </el-form-item>-->
            <!-- <el-form-item label="系统编号" prop="applicationCode">
          <el-input v-model="form.applicationCode" placeholder="请输入系统编号" />
        </el-form-item>
        <el-form-item label="所属机构编码" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入所属机构编码" />
            </el-form-item>-->
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  createCourse,
  updateCourse,
  deleteCourse,
  getCourse,
  getCoursePage,
  exportCourseExcel
} from "@/api/exam/course";
import { getSimpleList } from "@/api/examination/subjectCategory";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import ImageUpload from "@/components/ImageUpload";
import Editor from "@/components/Editor";
import FileUpload from "@/components/FileUpload";

export default {
  name: "Course",
  components: {
    ImageUpload,
    Treeselect,
    Editor,
    FileUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        categoryId: null,
        knowledgeName: null,
        isPay: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "只是类型不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "所属类目不能为空", trigger: "blur" }
        ],
        knowledgeName: [
          { required: true, message: "课程名称不能为空", trigger: "blur" }
        ]
        // imgUrl: [{required: true, message: "封面不能为空", trigger: "blur"}],
        // videoUrl: [{required: true, message: "请选择文件", trigger: "blur"}],
        // endTime: [{required: true, message: "到期时间不能为空", trigger: "blur"}],
        // knowledgeDesc: [{required: true, message: "到期时间不能为空", trigger: "blur"}],

        // status: [{ required: true, message: "审核状态（0通过、1审核中、2、拒绝）不能为空", trigger: "blur" }],
        // isPay: [{ required: true, message: "是否付费不能为空", trigger: "blur" }],
        // amount: [{ required: true, message: "金额不能为空", trigger: "blur" }],
        // createDate: [{ required: true, message: "创建时间不能为空", trigger: "blur" }],
        // modifyDate: [{ required: true, message: "修改时间不能为空", trigger: "blur" }],
      },
      //分类树
      cateOptions: undefined,
      categoryName: undefined,
      defaultProps: {
        children: "children",
        label: "categoryName"
      },
      imgShow: 0,
      videoShow: 0,
      fileType: ["mp4", "flv", "avi"],
      fileSize: 500
    };
  },
  watch: {
    // 根据名称筛选分类
    categoryName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getCoursePage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = parseInt(response.data.total);
        this.loading = false;
      });
    },
    /** 查询分类树形 */
    getTreeselect() {
      getSimpleList().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          response.data[i].id = parseInt(response.data[i].id);
          response.data[i].parentId = parseInt(response.data[i].parentId);
          response.data[i].categoryName =
            response.data[i].categoryName + "  - 类目ID:" + response.data[i].id;
        }
        // 处理 cateOptions 参数
        this.cateOptions = [];
        this.cateOptions.push(...this.handleTree(response.data, "id"));
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        categoryId: undefined,
        knowledgeName: undefined,
        knowledgeDesc: undefined,
        fileId: undefined,
        status: undefined,
        videoUrl: undefined,
        imgUrl: undefined,
        type: undefined,
        endTime: undefined,
        customCount: undefined,
        teacher: undefined,
        isPay: undefined,
        amount: undefined,
        isShow: 0,
        isPulice: undefined,
        count: undefined,
        reviewer: undefined,
        createDate: undefined,
        modifier: undefined,
        modifyDate: undefined,
        applicationCode: undefined,
        deptId: undefined
      };
      this.resetForm("form");
      this.imgShow = 0;
      this.videoShow = 0;
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.categoryName.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.categoryId = data.id;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getCourse(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程";
        this.form.type == 0 ? (this.videoShow = 1) : (this.imgShow = 1);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.deptId = this.$store.getters.deptId;
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateCourse(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createCourse(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal
        .confirm('是否确认删除课程编号为"' + id + '"的数据项?')
        .then(function() {
          return deleteCourse(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = { ...this.queryParams };
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal
        .confirm("是否确认导出所有课程数据项?")
        .then(() => {
          this.exportLoading = true;
          return exportCourseExcel(params);
        })
        .then(response => {
          this.$download.excel(response, "课程.xls");
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    // 格式化部门的下拉框
    normalizer(node) {
      return {
        id: node.id,
        label: node.categoryName,
        children: node.children
      };
    },
    //选择知识类型
    typeChange(e) {
      if (e == 0) {
        this.videoShow = 1;
        this.imgShow = 0;
      } else if (e == 1) {
        this.videoShow = 0;
        this.imgShow = 1;
      }
    }
  }
};
</script>
